package com.apestech.framework.xml.condition.modul;

import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.xml.table.modul.Tables;

import java.util.ArrayList;

/**
 * @author hup
 * @create 2019-04-11-17:07
 */
public class Report {
    public class FinalUpdate{
        private String type;
        private String sql;
        private String name;
        private String tableName;

        public FinalUpdate(JSONObject map){
            type=map.getString("type");
            sql=map.getString("sql");
            name=map.getString("name");
            tableName=map.getString("tableName");
        }

        public String getType() {
            return type;
        }

        public String getSql() {
            return sql;
        }

        public String getName() {
            return name;
        }

        public String getTableName() {
            return tableName;
        }
    }

    public class Column{
        private String id;
        private String name;
        private String modul;
        private String sql;
        private String from;

        public void setColumn(JSONObject map){
            id=map.getString("id");
            name=map.getString("name");
            modul=map.getString("modul");
            sql=map.getString("sql");
            from=map.getString("from");
        }

        public String getId() {
            return id;
        }

        public String getName() {
            return name;
        }

        public String getModul() {
            return modul;
        }

        public String getSql() {
            return sql;
        }

        public String getFrom() {
            return from;
        }
    }

    public class SumColumn{
        private String field;
        private String name;
        private String sql;
        public SumColumn(JSONObject map){
            field=map.getString("field");
            name=map.getString("name");
            sql=map.getString("sql");
        }

        public String getField() {
            return field;
        }

        public String getName() {
            return name;
        }

        public String getSql() {
            return sql;
        }
    }

    public class OutField{
        private String field;
        private String result;
        private String express;
        private String input;
        private String from;

        public OutField(JSONObject map){
            field=map.getString("field");
            result=map.getString("result");
            input=map.getString("input");
            express=map.getString("express");
            from=map.getString("from");
        }

        public String getField() {
            return field;
        }

        public String getResult() {
            return result;
        }

        public String getExpress() {
            return express;
        }

        public String getInput() {
            return input;
        }

        public String getFrom() {
            return from;
        }
    }

    public class Fields{
        private String type;
        private String id;
        private String inputs;
        private String exp;
        private String field;
        private ArrayList<OutField> outfield=new ArrayList<OutField>();

        public Fields(JSONObject map){
            type=map.getString("type");
            id=map.getString("id");
            inputs=map.getString("inputs");
            exp=map.getString("express");
            field=map.getString("field");
        }

        public void addOutField(JSONObject map){
            outfield.add(new OutField(map));
        }

        public String getType() {
            return type;
        }

        public String getId() {
            return id;
        }

        public String getInputs() {
            return inputs;
        }

        public String getExp() {
            return exp;
        }

        public String getField() {
            return field;
        }

        public ArrayList<OutField> getOutfield() {
            return outfield;
        }
    }

    public class Generate{
        private String dataField;
        private String empty;
        private JSONObject modulSql=new JSONObject();
        private ArrayList<SumColumn> sumColumn=new ArrayList<SumColumn>();
        private ArrayList<Fields> fields=new ArrayList<Fields>();

        public void setGenerate(JSONObject map){
            dataField=map.getString("dataField");
            empty=map.getString("empty");
        }

        public void addModeulSql(String id, String sql){
            modulSql.put(id, sql);
        }

        public String getModulSql(String id){
            return modulSql.getString(id);
        }

        public void addFields(Fields field){
            fields.add(field);
        }

        public ArrayList<Fields> getFields() {
            return fields;
        }

        public String getDataField() {
            return dataField;
        }

        public String getEmpty() {
            return empty;
        }

        public void addSumColumn(JSONObject map){
            sumColumn.add(new SumColumn(map));
        }

        public ArrayList<SumColumn> getSumColumn() {
            return sumColumn;
        }
    }

    private Generate generate=new Generate();
    private JSONArray lineList=new JSONArray();
    private ArrayList<FinalUpdate> finalUpdate=new ArrayList<FinalUpdate>();
    private ArrayList<FinalUpdate> iniUpdate=new ArrayList<FinalUpdate>();
    private Tables table;
    private String id;
    private String tableName;
    private String name;

    public void setBase(JSONObject map){
        id=map.getString("id");
        tableName=map.getString("tableName");
        name=map.getString("name");
    }

    public void addGenerate(JSONObject map){
        generate.setGenerate(map);
    }

    public void addModeulSql(String id, String sql){
        generate.addModeulSql(id, sql);
    }

    public void addColumn(int index, String id, JSONObject map){
        JSONObject m=getLine(index);
        Column col= (Column) m.get(id);
        if(col==null) col=new Column();
        col.setColumn(map);
        m.put(id, col);
        setLine(index, m);
    }

    public Tables getTable() {
        return table;
    }

    public void setTable(Tables table) {
        this.table = table;
    }

    public Column getColumn(int index, String id){
        if(index<0) index=0;
        JSONObject m=getLine(index);
        return (Column) m.get(id);
    }

    public Column getColumn(String id){
        return getColumn(-1, id);
    }

    public ArrayList<String> getColList(int index){
        if(index<0) index=0;
        JSONObject m=getLine(index);
        return m.getKeylist();
    }

    public ArrayList<String> getColList(){
        return getColList(-1);
    }

    public int line(){
        return lineList.size();
    }

    public String getModulSql(String id){
        return generate.getModulSql(id);
    }

    public String getDataField() {
        return generate.getDataField();
    }

    public String getEmpty() {
        return generate.getEmpty();
    }

    public void addFinalUpdate(JSONObject map){
        finalUpdate.add(new FinalUpdate(map));
    }

    public void addIniUpdate(JSONObject map){
        iniUpdate.add(new FinalUpdate(map));
    }

    public void addSumColumn(JSONObject map){
        generate.addSumColumn(map);
    }

    public ArrayList<SumColumn> getSumColumn() {
        return generate.getSumColumn();
    }

    public String getId() {
        return id;
    }

    public String getTableName() {
        return tableName;
    }

    public String getName() {
        return name;
    }

    public void addFields(Fields field){
        generate.addFields(field);
    }

    public ArrayList<Fields> getField(){
        return generate.getFields();
    }

    public Fields newFields(JSONObject map){
        return new Fields(map);
    }

    public ArrayList<FinalUpdate> getFinalUpdate() {
        return finalUpdate;
    }

    public ArrayList<FinalUpdate> getIniUpdate() {
        return iniUpdate;
    }

    private JSONObject getLine(int index){
        if(lineList.size()>index+1) return lineList.get(index);
        else return new JSONObject();
    }

    private void setLine(int index, JSONObject m){
        if(lineList.size()>index+1) lineList.set(index, m);
        else lineList.add(m);
    }

}
